<?php
/**
 * 积分类
 *
 */
class pointsHandler {
	
    private $dbca;       	//数据库连接
	    
    /**
     * 构造函数
     * @param DBCA $dbca 数据库对象
     */
    function __construct( $dbca ){
        $this->dbca = $dbca;
    }
    
    /**
     * 获取产品分类
     *
     */
    public function searchMidCategory() {
    	
    	//分类数组
    	$midCateList = array();
    	
    	//获取积分价格大于0的商品分类
    	$sql = 'SELECT DISTINCT(midCategoryID),midCategoryName,bigCategoryID,bigCategoryName 
    				FROM goods_list WHERE addPoints > 0 AND forsale = 1 AND pointsByFlg = 1;';
    	$midCate = &$this->dbca->stmt_query( $sql );
    	
    	if (!empty($midCate)) {
    		foreach ($midCate as $cate) {
    			$midCateList[] = array(
    				'bigCategoryID' => $cate->bigCategoryID,
    				'bigCategoryName' => $cate->bigCategoryName,
    				'midCategoryID' => $cate->midCategoryID,
    				'midCategoryName' => $cate->midCategoryName,
    				'categoryName' => $cate->bigCategoryID.'_'.$cate->midCategoryID.'_0',
    				'link' => '/shop/pointsMall-'.$cate->bigCategoryID.'_'.$cate->midCategoryID.'_0-0-0-0-1-sale-desc-1.html'
    			);
    		}
    	}
    	
    	return $midCateList;
    }
    
    /**
     * 获取积分活动形式
     *
     * @return array
     */
    public function searchActivityType() {
    	
    	//积分活动形式
		$activityType = array(
			'exchange' => array(
				'name' => '积分兑换',
				'link' => '/shop/pointsMall-0_0_0-1-0-0-1-sale-desc-1.html'
			),
			'pointsBuy' => array(
				'name' => '积分换购',
				'link' => '/shop/pointsMall-0_0_0-2-0-0-1-sale-desc-1.html'
			)
		);
		
		return $activityType;
    }
    
    /**
     * 获取积分范围
     *
     */
    public function searchPointsRange() {
    	
    	$pointsRange = array(
    		array('name' => '积分范围','value' => '0'),
    		array('name' => '1000以下','value' => '1'),
    		array('name' => '1001-2000','value' => '1001'),
    		array('name' => '2001-3000','value' => '2001'),
    		array('name' => '3001-4000','value' => '3001'),
    		array('name' => '4001-5000','value' => '4001'),
    		array('name' => '5001-10000','value' => '5001'),
    		array('name' => '10000以上','value' => '10001')
    	);
    	
    	return $pointsRange;
    }
    
    /**
     * 查询商品
     * array (
     * 		'uid' => '0_0_0',			//分类
     * 		'activityType' => 0,		//活动类型
     * 		'range' => 1001,			//积分范围
     * 		'buyFlg' => 0,				//我能兑换
     * 		'sortby' => 'sale',			//排序方式
     * 		'sort' => 'desc',			//排序类型
     * 		'pageIndex' => 1			//页码
     * )
     */
    public function searchGoods( $params ) {
    	
    	//实例化
    	$commHandler = new commHandler( $this->dbca );
    	
    	//实例化商品类
    	$goodsHandler = new goodsHandler( $this->dbca );
    	
    	//参数数组
    	$arg_array = array(
    		'useFlg' => 1,
    		'forsale' => 1,
    		'rp' => 20,
    		'page' => $params['pageIndex'],
    		'extendSql' => ' AND addPoints > 0 AND pointsByFlg = 1'		//扩展查询
    	);
    	
    	
    	//分类
    	if (isset($params['uid']) && !empty($params['uid'])) {
    		$cateArr = explode( '_', $params['uid']);
    		
    		//大分类
    		if ($cateArr[0]) {
    			$arg_array['bigCate'] = $cateArr[0];
    		}
    		
    		//中分类
    		if ($cateArr[1]) {
    			$arg_array['midCate'] = $cateArr[1];
    		}
    		
    		//小分类
    		if ($cateArr[2]) {
    			$arg_array['smallCate'] = $cateArr[2];
    		}
    	}
    	
    	//积分活动形式
    	if (isset($params['activityType']) && $params['activityType']) {
    		
    		if ($params['activityType'] == 1) { //积分兑换
    			
    			$arg_array['extendSql'] .= ' AND pointPrice = 0';
    		
    		} else if($params['activityType'] == 2) { //积分换购
    			
    			$arg_array['extendSql'] .= ' AND pointPrice > 0';
    		}
    		
    	}
    	
    	//积分范围
    	if (isset($params['range']) && !empty($params['range'])) {
    		switch ($params['range']) {
    			case 1:
    				$arg_array['extendSql'] .= ' AND addPoints > 0 AND addPoints <= 1000';
    				break;
    			case 1001:
    				$arg_array['extendSql'] .= ' AND addPoints > 1000 AND addPoints <= 2000';
    				break;
    			case 2001:
    				$arg_array['extendSql'] .= ' AND addPoints > 2001 AND addPoints <= 3000';
    				break;
    			case 3001:
    				$arg_array['extendSql'] .= ' AND addPoints > 3001 AND addPoints <= 4000';
    				break;
    			case 4001:
    				$arg_array['extendSql'] .= ' AND addPoints > 4001 AND addPoints <= 5000';
    				break;
    			case 5001:
    				$arg_array['extendSql'] .= ' AND addPoints > 5001 AND addPoints <= 6000';
    				break;
    			case 10001:
    				$arg_array['extendSql'] .= ' AND addPoints > 6000';
    				break;
    		}
    	}
    	
    	//我能兑换的产品
    	if (isset($params['buyFlg']) && $params['buyFlg'] == 1) {
    		$arg_array['extendSql'] .= " AND addPoints <= '".$params['buyFlg']."'";
    	}
    	
    	//排序方式
    	if (isset($params['sortby']) && in_array($params['sortby'],array('sale','price','update','discount'))) {
    		
    		switch ($params['sortby']) {									//排序方式
		        case 'price':
		            $arg_array['sortname'] = 'addPoints';
		            break;
		        case 'update':
		            $arg_array['sortname'] = 'timeStamp';
		            break;
		        case 'discount':
		            $arg_array['sortname'] = 'discount';
		            break;
		        case 'sale':
		            $arg_array['sortname'] = 'saletotal';
		            break;
		    }
		    
		    //排序类型
		    $arg_array['sortorder'] = $params['sort'];
    	}
    	
    	$goodsList = $goodsHandler->searchGoodsList( $arg_array );
    	
		return array( 'goodsList' => $goodsList, 'count' => $arg_array['count'], 'rp' => $arg_array['rp'] );
    }
	
    /**
     * 获取积分销售排行
     * @param int $limit 查询商品数量
     */
    public function getSaleTop( $limit ) {
    	
    	$goodsHandler = new goodsHandler( $this->dbca );
    	
    	$arg_array = array(
    		'useFlg' => 1,
    		'forsale' => 1,
    		'pointsByFlg' => 1,
    		'sortname' => 'saletotal',
    		'sortorder' => 'desc',
    		'page' => 1,
    		'rp' => $limit,
    		'extendSql' => ' AND addPoints > 0'
    	);
    	
    	$topGoodsList = $goodsHandler->searchGoodsList($arg_array);
    	
    	return $topGoodsList;
    }
    
}